//Copyright © 2012 Ryan Larson, Ben Glasser
//[This program is licensed under the "GPL v3.0 License"]
// Please see the file COPYING in the source
//distribution of this software for license terms.
package org.app_chat.activemq.integration;

import com.google.protobuf.Message;
import org.app_chat.activemq.Test;
import org.app_chat.api.Listener;
import org.app_chat.activemq.ChatRoom;
import org.junit.Ignore;

import java.util.concurrent.Callable;

import static com.jayway.awaitility.Awaitility.await;

/**
 * Created by Ryan of app-chat.org
 * on: 7/14/12 at 5:47 PM
 */
public class Integration {

    private boolean messageReceived = false;

    /*
    We don't want this test to be run with the build because it depends on the
    activemq broker service running. This is purely an integration test to make
    sure everything is working end to end, and equally a code example to show
    you how you might set up a simple broker using the default broker URL.
     */
    @Ignore
    @org.junit.Test
    public void testCanAddListenerAndReceiveMessage() throws Exception {


        Message protobufMessage = Test.SearchRequest
                .newBuilder()
                .setTest("protowin")
                .build();

        ChatRoom chatRoom = ChatRoom.builder()
                .listenFor(new TestingListener())
                .build();

        chatRoom.startChat();

        chatRoom.getPublisher().publish(protobufMessage);

        await().until(messageIsReceived());
    }

    private Callable<Boolean> messageIsReceived() {
        return new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return messageReceived;
            }
        };
    }

    private class TestingListener implements Listener<Test.SearchRequest> {

        @Override
        public void messageReceived(Test.SearchRequest message) {
            System.out.println("Integration Test Message Received");
            messageReceived = true;
        }

        @Override
        public String getQueueName() {
            return Test.SearchRequest.getDescriptor().getFullName();
        }
    }
}
